import requestApi from '@/api/index.js'
import { Message, MessageBox } from 'element-ui'
import { local as storage } from '@/utils/storage' // 验权

const user = {
  state: {
    token: storage.get('token'),
    name: '',
    avatar: '',
    phone: '',
    status: '',
    roles: ''
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_STATUS: (state, status) => {
      state.status = status
    },
    SET_PHONE: (state, phone) => {
      state.phone = phone
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim();
      return new Promise((resolve, reject) => {
        requestApi.login(userInfo).then(response => {
          const data = response;
          if (data.code == 0) {
            const tokenStr = data.token;
            storage.set('token', tokenStr);
            commit('SET_ROLES', data.roles);
            commit('SET_TOKEN', tokenStr);
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 验证token
    CheckSession({ commit, state }) {
      const param = { sessionId: state.token }
      return new Promise((resolve, reject) => {
          requestApi.checkSession(param).then(response => {
            const data = response;
            if(data.code == 0) {
              resolve(true)
            } else {
              Message({
                message: data.message,
                type: 'error',
                duration: 3 * 1000
              })
              commit('SET_TOKEN', '');
              storage.remove('token');
              resolve(false)
            }
          })
      }).catch((error) => {
        reject(error)
      })
    },

    // 获取用户信息
    GetInfo({ commit }) {
      return new Promise((resolve, reject) => {
        requestApi.getInfo().then(response => {
          if (response.code == 0) {
            const data = response.result;
            commit('SET_NAME', data.name);
            commit('SET_AVATAR', data.avatar);
            commit('SET_STATUS', data.status);
            commit('SET_PHONE', data.phone);
            resolve(true)
          } else {
            Message({
              message: response.message,
              type: 'error',
              duration: 3 * 1000
            })
            commit('SET_NAME', '');
            commit('SET_AVATAR','');
            commit('SET_STATUS', '');
            commit('SET_PHONE','');
            resolve(false)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        requestApi.logout(state.token).then(() => {
          commit('SET_NAME', '')
          commit('SET_AVATAR', '')
          commit('SET_STATUS', '')
          commit('SET_PHONE', '')
          remove('token')
          commit('SET_TOKEN', '');
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '');
        storage.remove('token');
        resolve()
      })
    }
  }
}

export default user
